package com.talkweb.osmharmony.views.overlay.gestures;

import com.talkweb.osmharmony.views.MapView;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;


/**
 * heads up, this class is used internally by osmdroid, you're welcome to use but it the interface
 * {@link RotationListener} will not fire as expected. It is used internally by osmdroid. If you want
 * to listen for rotation changes on the {@link MapView} then use MapView#setMapListener(MapListener}
 * and check for {@link MapView#getMapOrientation()}. See <a href="https://github.com/osmdroid/osmdroid/issues/628">https://github.com/osmdroid/osmdroid/issues/628</a>
 */
public class RotationGestureDetector {

    /**
     * heads up, this class is used internally by osmdroid, you're welcome to use but it the interface
     * {@link RotationListener} will not fire as expected. It is used internally by osmdroid. If you want
     * to listen for rotation changes on the {@link MapView} then use MapView#setMapListener(MapListener)}
     * and check for {@link MapView#getMapOrientation()}
     * See <a href="https://github.com/osmdroid/osmdroid/issues/628">https://github.com/osmdroid/osmdroid/issues/628</a>
     */
    public interface RotationListener {
        void onRotate(float deltaAngle);
    }

    protected float mRotation;
    private RotationListener mListener;
    private boolean mEnabled = true;

    public RotationGestureDetector(RotationListener listener) {
        mListener = listener;
    }

    private static float rotation(TouchEvent event) {
        MmiPoint firstPoint = event.getPointerPosition(0);
        MmiPoint lastPoint = event.getPointerPosition(1);
        double delta_x = (firstPoint.getX() - lastPoint.getX());
        double delta_y = (firstPoint.getY() - lastPoint.getY());
        double radians = Math.atan2(delta_y, delta_x);
        return (float) Math.toDegrees(radians);
    }

    public void onTouch(TouchEvent e) {
        if (e.getPointerCount() != 2)
            return;

        if (e.getAction() == TouchEvent.OTHER_POINT_DOWN) {
            mRotation = rotation(e);
        }

        float rotation = rotation(e);
        float delta = rotation - mRotation;

        //we have to allow detector to capture and store the new rotation to avoid UI jump when
        //user enables the overlay again
        if (mEnabled) {
            mRotation += delta;
            mListener.onRotate(delta);
        } else {
            mRotation = rotation;
        }
    }

    public void setEnabled(final boolean pEnabled) {
        this.mEnabled = pEnabled;
    }

    public boolean isEnabled() {
        return this.mEnabled;
    }
}
